package cn.safekeeper.core.manager;

import cn.safekeeper.common.model.LoginEvent;
import cn.safekeeper.common.model.LogoutEvent;
import cn.safekeeper.common.utils.SafeKeeperUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 事件处理组件
 * @author skylark
 */
public class SafeKeeperEventProcessor {
	/**
	 * 日志
	 */
	private static final Logger log = LoggerFactory.getLogger(SafeKeeperEventProcessor.class);
	/**
	 * 登录标记
	 */
	private static final String LOG_FLAG = "login";

	/**
	 * 处理登录事件
	 * @param loginEvent 登录事件
	 */
	public void loginEventHandle(LoginEvent loginEvent) {
		try {
			//如果已经登录了
			String loginFlag = SafeKeeperManager.getSafeKeeperTokenRealm().get(loginFlag(loginEvent));
			if (LOG_FLAG.equals(loginFlag)) {

			}

			//如果已经登录了就不在进行相关的逻辑
			if (SafeKeeperUtils.isEmpty(String.valueOf(loginEvent.getId())) || SafeKeeperUtils.isEmpty(loginEvent.getLoginType())) {
				//不进行任何处理
				return;
			}
			String userJsonInfo = SafeKeeperManager.getSafeKeeperAuthorizationCallBack().
					getUserJsonInfo(loginEvent.getId(), loginEvent.getLoginType());
			if (!SafeKeeperUtils.isEmpty(userJsonInfo)) {
				SafeKeeperManager.getSafeKeeperTokenRealm().set(getLoginKey(loginEvent), userJsonInfo, loginEvent.getTimeout());
				//登录标记
				SafeKeeperManager.getSafeKeeperTokenRealm().set(loginFlag(loginEvent), LOG_FLAG, loginEvent.getTimeout());
			}
		} catch (Exception e) {
			log.error("登录出现异常", e);
		}
	}

	/**
	 * 获取用户缓存数据
	 * @param id        业务id
	 * @param loginType 登录类型
	 * @return 用户对象json
	 */
	public String getUserSubject(Object id, String loginType) {
		LoginEvent loginEvent = new LoginEvent();
		loginEvent.setId(id);
		loginType = loginType == null ? SafeKeeperManager.getConfig().getDefaultLoginType() : loginType;
		loginEvent.setLoginType(loginType);
		String userJson = SafeKeeperManager.getSafeKeeperTokenRealm().get(getLoginKey(loginEvent));
		if (SafeKeeperUtils.isEmpty(userJson)) {
			String userJsonInfo = SafeKeeperManager.getSafeKeeperAuthorizationCallBack().getUserJsonInfo(id, loginType);
			if (null != userJsonInfo && !"".equals(userJsonInfo)) {
				loginEventHandle(loginEvent);
				return userJsonInfo;
			}
			return null;
		}
		return userJson;
	}

	/**
	 * 更新用户缓存
	 * @param id        业务id
	 * @param loginType 登录类型
	 */
	public void setUserSubject(Object id, String loginType, String userJsonInfo) {
		LoginEvent loginEvent = new LoginEvent();
		loginEvent.setId(id);
		loginType = loginType == null ? SafeKeeperManager.getConfig().getDefaultLoginType() : loginType;
		loginEvent.setLoginType(loginType);

		//判断是否已经登录
		String loginFlag = SafeKeeperManager.getSafeKeeperTokenRealm().get(loginFlag(loginEvent));
		if (LOG_FLAG.equals(loginFlag) && !SafeKeeperUtils.isEmpty(userJsonInfo)) {
			SafeKeeperManager.getSafeKeeperTokenRealm().set(getLoginKey(loginEvent), userJsonInfo, loginEvent.getTimeout());
		}
	}

	private String loginFlag(LoginEvent loginEvent) {
		return "login-flag:" + loginEvent.getLoginType() + loginEvent.getId();
	}

	/**
	 * key
	 * @param loginEvent 登录事件对象
	 * @return key
	 */
	private String getLoginKey(LoginEvent loginEvent) {
		return getReallyKey(loginEvent.getLoginType(), loginEvent.getId());
	}

	/**
	 * 通过登录类型业务id拼接key
	 *
	 * @param loginType 登录类型
	 * @param id        业务id
	 * @return key
	 */
	private String getReallyKey(String loginType, Object id) {
		return loginType.concat(":").concat(String.valueOf(id).concat(":info"));
	}

	/**
	 * key
	 *
	 * @param logoutEvent 注销事件对象
	 * @return key
	 */
	private String getLogoutKey(LogoutEvent logoutEvent) {
		return getReallyKey(logoutEvent.getLoginType(), logoutEvent.getId());
	}

	/**
	 * 注销处理
	 *
	 * @param logoutEvent 注销事件对象
	 */
	public void logoutEventHandle(LogoutEvent logoutEvent) {
		try {
			if (null == logoutEvent || SafeKeeperUtils.isEmpty(String.valueOf(logoutEvent.getId()))
					|| SafeKeeperUtils.isEmpty(logoutEvent.getLoginType())) {
				//不进行任何处理
				return;
			}
			SafeKeeperManager.getSafeKeeperTokenRealm().delete(getLogoutKey(logoutEvent));
			SafeKeeperManager.getSafeKeeperTokenRealm().clearRoleOrPermissionCacheData(logoutEvent.getId(),
					logoutEvent.getLoginType(), 0);
			SafeKeeperManager.getSafeKeeperTokenRealm().clearRoleOrPermissionCacheData(logoutEvent.getId(),
					logoutEvent.getLoginType(), 1);
		} catch (Exception e) {
			log.error("登录出现异常", e);
		}
	}

}
